/**
 * Copyright 2010 VTT Finnish Technical Research Centre
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.osami.syncagent.server.tcpip;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

import org.osami.syncagent.server.SyncAgentServer;

public class SyncAgentServerListenerTCPIP implements Runnable{
	private Socket server;
	private String input;
	private Object line;
	private SyncAgentServer agentService;
	
	public SyncAgentServerListenerTCPIP(Socket server, SyncAgentServerTCPIP service)
	{
		this.server=server;
		this.agentService = service;
	}
	/**
	 * run listens for new clients and adds them in the list
	 */
	@Override
	public void run() {
	      input="";

	      try {
	        // Get input from the client
	        DataInputStream in = new DataInputStream (server.getInputStream());
	        //PrintStream out = new PrintStream(server.getOutputStream());

	        while((line = in.readLine()) != null && !line.equals(".")) {
	          input=input + line;
	          System.out.println("I got:" + line);
	        }
	        System.out.println("Overall message is:" + input);
	        agentService.interpretMessage(input);

	        server.close();
	      } catch (IOException ioe) {
	        System.out.println("IOException on socket listen (TCP): " + ioe);
	        ioe.printStackTrace();
	      }
	}

}
